//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Serialization;
using System.Diagnostics;

namespace OpenLS.Chart
{
    public class Line3DChart : LineChartBase, I3DChart
    {
        private Axis _firstAxid;
        private int _gapDepth = 150;
        private Axis _secondAxis;
        private Axis _thirdAxis;

        public Line3DChart()
        {
            FirstAxis = new ValueAxis {Position = AxisPosition.Left};
            SecondAxis = new CategoryAxis();
            ThirdAxis = new SeriesAxis();
            FirstAxis.CrossAxis = SecondAxis;
            SecondAxis.CrossAxis = FirstAxis;
            ThirdAxis.CrossAxis = SecondAxis;
        }
        public Axis VerticalAxis
        {
            get
            {
                return Enumerable.FirstOrDefault(GetAxes(),
                                                 a =>
                                                 a.Position == AxisPosition.Left || a.Position == AxisPosition.Right);            }
        }

        public Axis ThirdAxis
        {
            get { return _thirdAxis; }
            set { _thirdAxis = value; }
        }

        public Axis SecondAxis
        {
            get { return _secondAxis; }
            set { _secondAxis = value; }
        }

        public Axis FirstAxis
        {
            get { return _firstAxid; }
            set { _firstAxid = value; }
        }


        public int GapDepth
        {
            get { return _gapDepth; }
            set { _gapDepth = value; }
        }


        protected override void VerifyAxes()
        {
            Debug.WriteLine("todo //\\" + GetType());
        }

        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("line3DChart"))
            {
                ReadXmlBase(c);
                GapDepth = c.ReadOptionalElementIntegerAttribute("gapDepth", 150);
                AxisResolver r = ReadContextData.GetAxisResolver(c);
                r.AddAction(c.ReadElementUIntegerAttribute("axId"), delegate(Axis a) { FirstAxis = a; });
                r.AddAction(c.ReadElementUIntegerAttribute("axId"), delegate(Axis a) { SecondAxis = a; });
                r.AddAction(c.ReadElementUIntegerAttribute("axId"), delegate(Axis a) { ThirdAxis = a; });
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("line3DChart"))
            {
                WriteXmlBase(c);
                c.WriteOptionalElementIntegerAttribute("gapDepth", GapDepth, 150);
                c.WriteElementUIntegerAttribute("axId", FirstAxis.Id);
                c.WriteElementUIntegerAttribute("axId", SecondAxis.Id);
                c.WriteElementUIntegerAttribute("axId", ThirdAxis.Id);
                c.WriteOptionalElement(Extension);
            }
        }

        internal override IEnumerable<Axis> GetAxes()
        {
            yield return FirstAxis;
            yield return SecondAxis;
            yield return ThirdAxis;
        }
    }
}